package com.pla.googlecalendarviews;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import org.wonderly.awt.Packer;

public class AccountDialog extends JDialog {

  private JButton submitButton = new JButton("Submit");
  private JButton cancelButton = new JButton("Cancel");
  private JTextField emailAddressField = new JTextField(30);
  private JPasswordField passwordField = new JPasswordField(30);
  private JFrame parent;

  public AccountDialog(JFrame newParent, final Account account) {
    parent = newParent;
    setTitle("Account Maintenance");
    Utils.makeDisposeOnEscapeKey(this);
    setModal(true);
    JPanel panel = new JPanel();
    Packer packer = new Packer(panel);
    int y = 0;
    packer.pack(new JLabel("Email address")).gridx(0).gridy(y).west();
    packer.pack(emailAddressField).gridx(1).gridy(y++).west();
    emailAddressField.setText(account.getEmailAddress());
    packer.pack(new JLabel("Password")).gridx(0).gridy(y).west();
    packer.pack(passwordField).gridx(1).gridy(y++).west();
    passwordField.setText(account.getPassword());
    packer.pack(submitButton).gridx(0).gridy(y);
    packer.pack(cancelButton).gridx(1).gridy(y++);
    cancelButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        dispose();
      }
    });
    submitButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        account.setEmailAddress(emailAddressField.getText());
        account.setPassword(new String(passwordField.getPassword()));
        validateAccount(account);
      }
    });
    submitButton.setMnemonic(KeyEvent.VK_S);
    cancelButton.setMnemonic(KeyEvent.VK_C);
    setContentPane(panel);
    pack();
    setLocationRelativeTo(parent);
    if (account.isExists()) {
      emailAddressField.setEnabled(false);
      passwordField.requestFocusInWindow();
    } else {
      emailAddressField.requestFocusInWindow();
    }
    setVisible(true);
  }

  private void validateAccount(final Account account) {
    if (Utils.isBlank(emailAddressField.getText())) {
      JOptionPane.showMessageDialog(parent, "Please type an email address", "Error", JOptionPane.ERROR_MESSAGE);
      return;
    }
    if (Utils.isBlank(new String(passwordField.getPassword()))) {
      JOptionPane.showMessageDialog(parent, "Please type a password", "Error", JOptionPane.ERROR_MESSAGE);
      return;
    }
    submitButton.setEnabled(false);
    cancelButton.setEnabled(false);
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    SwingWorker worker = new SwingWorker() {

      private boolean valid = false;

      protected Object doInBackground() throws Exception {
        valid = Utils.isCredentialsValid(account.getEmailAddress(), account.getPassword());
        return null;
      }

      protected void done() {
        if (valid) {

          AccountDAO accountDAO = new AccountDAO();
          accountDAO.update(account);
          dispose();
        } else {
          JOptionPane.showMessageDialog(parent, "User name and/or password not correct for: " + emailAddressField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
          submitButton.setEnabled(true);
          cancelButton.setEnabled(true);
          setCursor(null);
        }
      }
    };
    worker.execute();
  }
}
